package com.hy.main.demo.tree;

import cn.hutool.core.io.IoUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.util.Assert;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author yang DongYong
 * @Date 2022/4/30 - 04 - 30 - 23:12
 * @Description: com.hy.main.demo.recursion
 * @version: 1.0
 */
@Slf4j
public class HuToolTreeDemo {


    @Test
    public void tree() {
        List<Menu> menus = Arrays.asList(
                new Menu(1, "新手专区", 0),
                new Menu(2, "指南", 1),
                new Menu(3, "礼包", 1),
                new Menu(4, "角色装备", 2),
                new Menu(5, "角色咨询", 2),
                new Menu(6, "角色详情", 2),
                new Menu(7, "服装礼包", 3),
                new Menu(8, "装备礼包", 3),
                new Menu(9, "金币礼包", 3)
        );


        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("order");
        treeNodeConfig.setIdKey("rid");
        // 最大递归深度
//        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getParentId().toString());
//                    tree.setWeight(treeNode.getWeight());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("extraField", getId(treeNode.getId()));
                    tree.putExtra("other", new Object());
                });
        System.out.println(JSONObject.toJSON(treeNodes));
        System.out.println(treeNodes.get(0));
    }


    public static int getId(int id) {

        return id;
    }

    @Test
    public void ioTest() throws FileNotFoundException {

        IoUtil.getReader(new FileInputStream("d://ceShi.txt"),"gbk");
    }

    public static void main(String[] args) {
        String in = null;
        try {
            Assert.notNull(in, "InputStream is null !");
        }catch (Exception e) {
            log.error(e.getMessage(),e);
            e.printStackTrace();
        }

    }
}
